function findRPList() {
    var y = $("#reward_year").val();
    var m = $("#reward_month").val();
    var sId = $("#admin_reward").find("#employee_sections").val();
    var pId = $("#admin_reward").find("#employee_positions").val();

    var $tb = $("#admin_reward_table").find("tbody");

    $.post("../rp/findRPList",
        {"year": y, "month": m, "sectionId": sId, "positionId": pId},
        function (data) {
            console.log("year = " + y + "---month = " + m + "---sId = " + sId + "---pId = " + pId);
            if ($.isEmptyObject(data)) {
                alert("还没有相关奖惩信息！");
            } else {
                $tb.html("");
                for (var i = 0, n = data.length; i < n; i++) {
                    $tb.append("<tr><td>" + data[i].id + "</td>" +
                        "<td>" + data[i].resume.realName + "</td>" +
                        "<td>" + data[i].cause + "</td>" +
                        "<td>" + new Date(data[i].rpDate).Format("yyyy-MM-dd") + "</td>" +
                        "<td>" + data[i].money + "</td>" +
                        "<td>" + (data[i].rewardOrPunishment === 1 ? "奖励" : "处罚") + "</td></tr>");
                }
                $tb.find("td").css({"text-align": "center", "padding": "3px 0px"});
            }
        });
}

function judgeOperation() {
    var op = $("#salary_operation").val();
    var $y = $("#salary_year");
    var $m = $("#salary_month");
    var $s = $("#salary_section");
    var $p = $("#salary_position");
    var $r = $("#salary_resume");
    $("#admin_salary_table").find("tfoot").hide();
    switchoverColor(this, $("#admin_salary"));
    if (op === "0") {                 //工资查询操作
        $y.show();
        $m.show();
        selectYMD($y, $m);
        $("#admin_salary_table").find("tbody").html("").append("<tr><td colspan='9'>支持按月份、部门、职位、员工查询</td></tr>");
    } else {
        $y.hide();
        $m.hide();
        $s.show();
        $p.show();
        $r.show();
    }
}

function findSalaryList() {
    var op = $("#salary_operation").val();
    var y = $("#salary_year").val();
    var m = $("#salary_month").val();
    var sId = $("#salary_section").val();
    var pId = $("#salary_position").val();
    var rId = $("#salary_resume").val();
    var $tb = $("#admin_salary_table").find("tbody");
    var $tf = $("#admin_salary_table").find("tfoot");
    var d = new Date().getDate();
    $tf.hide();
    if (sId === "") {
        alert("请选择部门！");
    } else if (op === "0" && sId !== "") {     //查询操作
        $.post("../salary/findSalaryList",
            {"year": y, "month": m, "sectionId": sId, "positionId": pId, "resumeId": rId},
            function (data) {
                if ($.isEmptyObject(data))
                    $tb.html("").append("<tr><td colspan='9' style='text-align: center'>还没有相关消息！</td></tr>");
                else {
                    $tb.html("");
                    for (var i = 0, n = data.length; i < n; i++) {
                        var date = new Date(data[i].grantDate);
                        var m = date.getMonth() + 1;
                        if (m < 10)
                            m = "0" + m;
                        $tb.append("<tr><td>" + data[i].id + "</td>" +
                            "<td>" + data[i].entryTable.resume.realName + "</td>" +
                            "<td>" + data[i].finalPay + "</td>" +
                            "<td>" + data[i].entryTable.positionLevel.regularPay + "</td>" +
                            "<td>" + data[i].performance.performance + "</td>" +
                            "<td>" + data[i].extraWorkPay + "</td>" +
                            "<td>" + data[i].rewardPunishment + "</td>" +
                            "<td>" + data[i].entryTable.positionLevel.socialSecurity + "</td>" +
                            "<td>" + date.getFullYear() + "-" + m + "</td></tr>");
                    }
                }
            });
    }
    else if (op === "1" && d < 20) {        //批量添加（工资发放）操作
        alert("工资每月20号以后统一发放！");
    }
    else {
        $.post("../salary/findSalaryList",
            {"sectionId": sId, "positionId": pId, "resumeId": rId, "status": 1},
            function (data) {
                if ($.isEmptyObject(data)) {
                    getGrantList(sId, pId, rId, $tb, $tf);
                } else
                    $tb.html("").append("<tr><td colspan='9' style='text-align: center'>该部门工资已发放！</td></tr>");
            });
    }
}

function getGrantList(sId, pId, rId, tb, tf) {
    var isShow;
    $.post("../admin/salaryList",
        {"sectionId": sId, "positionId": pId, "resumeId": rId},
        function (data) {
            if ($.isEmptyObject(data))
                $(tb).html("").append("<tr><td colspan='9' style='text-align: center'>还没有相关消息！</td></tr>");
            else {
                $(tb).html("");
                for (var i = 0, n = data.length; i < n; i++) {
                    if (data[i].performance !== null) {
                        $(tb).append("<tr><td> </td>" +
                            "<td><input type='hidden' name='salaryList[" + i + "].entryTable.id' value='" + data[i].entryTable.id + "' readonly>" + data[i].entryTable.resume.realName + "</td>" +
                            "<td><input type='text' name='salaryList[" + i + "].finalPay' value='" + data[i].finalPay + "' readonly></td>" +
                            "<td>" + data[i].entryTable.positionLevel.regularPay + "</td>" +
                            "<td><input type='hidden' name='salaryList[" + i + "].performance.id' value='" + data[i].performance.id + "' class='perId'>" +
                            "<input type='text' name='salaryList[" + i + "].performance.performance' value='" + data[i].performance.performance + "' readonly></td>" +
                            "<td><input type='text' name='salaryList[" + i + "].extraWorkPay' value='" + data[i].extraWorkPay + "' readonly></td>" +
                            "<td><input type='text' name='salaryList[" + i + "].rewardPunishment' value='" + data[i].rewardPunishment + "' readonly></td>" +
                            "<td>" + data[i].entryTable.positionLevel.socialSecurity + "</td>" +
                            "<td><input type='text' name='salaryList[" + i + "].grantDate' class='grant_date' readonly></td></tr>");
                        isShow = 1;
                    } else {
                        $(tb).append("<tr><td colspan='9' style='text-align: center'>还有绩效未添加，请先通知相关部门领导添加绩效！</td></tr>");
                        isShow = 0;
                    }
                }
                if (isShow === 1)
                    $(tf).show();
                if (isShow === 0)
                    $(tf).hide();

                $(tb).find("td").css("text-align", "center");
                $(tb).find("input").css({
                    "text-align": "center",
                    "width": "60px",
                    "margin": "1px auto"
                });
                $(tb).find("input[type=text][class=grant_date]").each(function () {
                    $(this).val(getGrantDate());
                });
            }
        });
}

function getGrantDate() {
    var now = new Date();
    var y = now.getFullYear();
    var m = now.getMonth();
    if (m === 0) {
        m = 12;
        y -= 1;
    }
    if (m < 10)
        m = "0" + m;

    var dateStr = y + "-" + m;
    return dateStr;
}

function checkPerformance(ele) {
    if (confirm("您确认要发放吗？")) {
        // var flag = 0;
        // $(ele).find("input[class=perId][type=hidden]").each(function () {
        //     var perId = $(this).val();
        //     if (perId === "")
        //         flag = 1;
        // });
        // if (flag === 0) {
        $(ele).find("input[class=grant_date][type=text]").each(function () {
            var str = $(this).val();
            var grantStr = str + "-" + "20";
            $(this).val(grantStr);
        });
        return true;
        // }

        // if (flag === 1) {
        //     alert("还有绩效未添加，请先通知相关部门领导添加绩效！");
        //     return false;
        // }
    }
    return false;
}

function sectionChanged(ele) {
    var sId = $(ele).val();
    $.post("../admin/positions",
        {"sId": sId},
        function (data) {
            var $po = $("#salary_position");
            $po.html("<option value=''>职位</option>");
            for (var i = 0, n = data.length; i < n; i++) {
                $po.append("<option value='" + data[i].id + "'>" + data[i].position + "</option>");
            }
            $po.show();
        });
}

function positionChanged(ele) {
    var pId = $(ele).val();
    $.post("../admin/findResumeList",
        {"pId": pId},
        function (data) {
            var $sr = $("#salary_resume");
            $sr.html("<option value=''>员工</option>");
            for (var i = 0, n = data.length; i < n; i++) {
                $sr.append("<option value='" + data[i].id + "'>" + data[i].realName + "</option>");
            }
            $sr.show();
        });
}

function checkDissentDealForm() {
    var val = $("#deal").val();
    if (val === '') {
        alert("请输入处理意见！");
        return false;
    }
    return true;
}

function dissentDealReset() {
    if (confirm("您确定要放弃处理并退出吗？")) {
        $("#admin_deal_background").hide();
        $("#admin_deal_dissent").hide();
    }
}

function getDissentList() {
    var val = $("#admin_dissent_select").val();
    var $tb = $("#admin_dissent_table").find("tbody");
    $.post("../dissent/dissentList",
        {"looked": val},
        function (data) {
            if ($.isEmptyObject(data)) {
                $tb.html("").append("<tr><td colspan='5' style='text-align: center'>暂无相关消息！</td></tr>");
            } else {
                $tb.html("");
                if (val === "0") {
                    for (var i = 0, n = data.length; i < n; i++) {
                        $tb.append("<tr><td>" + data[i].id + "</td>" +
                            "<td>" + data[i].salarySheet.entryTable.resume.realName + "</td>" +
                            "<td>" + data[i].dissent + "</td>" +
                            "<td>" + new Date(data[i].salarySheet.grantDate).Format("yyyy-MM") + "</td>" +
                            "<td class='deal'><input type='hidden' name='dissentId' value='" + data[i].id + "'>处理</td></tr>")
                    }
                } else {
                    for (var j = 0, m = data.length; j < m; j++) {
                        $tb.append("<tr><td>" + data[j].id + "</td>" +
                            "<td>" + data[j].salarySheet.entryTable.resume.realName + "</td>" +
                            "<td>" + data[j].dissent + "</td>" +
                            "<td>" + new Date(data[j].salarySheet.grantDate).Format("yyyy-MM") + "</td>" +
                            "<td>已处理</td></tr>")
                    }
                }
                $tb.find("td").css({"text-align":"center","margin":"3px 16px"});
                var $deal = $("td[class=deal]");

                $deal.css({
                    "padding": "2px 12px",
                    "cursor": "pointer",
                    "color": "rgba(0,20,230,0.7)"
                });

                $deal.hover(function () {     //包含属性值并以空格隔开的选择器
                    $(this).css("color", "rgba(0,200,60,0.7)");
                }, function () {
                    $(this).css("color", "rgba(0,20,230,0.7)");
                });

                $deal.click(function () {
                    var dId = $(this).find("input[name=dissentId]").val();
                    $("#dissent_form").find("input[class=dissentId]").val(dId);
                    $("#admin_deal_background").show();
                    $("#admin_deal_dissent").show();
                });
            }
        });
}